package com.sofwin.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.AjaxResult;
import com.sofwin.system.mapper.SysUserMapper;
import com.sofwin.system.pojo.SysUser;
import com.sofwin.system.pojo.SysUserExample;
import com.sofwin.system.service.UserService;
import com.sofwin.system.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @packageName: com.sofwin.system.service.impl
 * @user: andyliu
 * @date: 2021/7/13 10:52
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private SysUserMapper mapper;
    @Override
    public PageInfo<SysUser> queryUserByPage(UserVO user, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        SysUserExample ex = new SysUserExample();
        SysUserExample.Criteria criteria = ex.createCriteria();
        if(user!=null){
            if(user.getLoginAccount()!=null && !"".equals(user.getLoginAccount())){
                criteria.andLoginAccountLike("%"+user.getLoginAccount()+"%");
            }
            if(user.getMobile()!=null && !"".equals(user.getMobile())){
                criteria.andMobileEqualTo(user.getMobile());
            }
            if(user.getStatus()!=null && -1!=user.getStatus()){
                criteria.andStatusEqualTo(user.getStatus());
            }
            if (user.getBeginDate() != null && !"".equals(user.getBeginDate())) {
                criteria.andCreateDateGreaterThanOrEqualTo(user.getBeginDate());
            }
            if(user.getEndDate()!=null && !"".equals(user.getEndDate())){
                criteria.andCreateDateLessThanOrEqualTo(user.getEndDate());
            }

        }
        List<SysUser> users = mapper.selectByExample(ex);
        return new PageInfo(users);
    }

    @Override
    public SysUser queryUserById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean saveOrUpdateUser(SysUser user) {
        if(user.getSysId()!=null){
            return mapper.updateByPrimaryKeySelective(user)==1?true:false;
        }
        return  mapper.insertSelective(user)==1?true:false;
    }

    @Override
    public boolean removeUserById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public boolean removeUserByIds(Integer[] ids) {
        int flag=0;
        for(Integer id:ids){
            if(removeUserById(id)) flag++;
        }

        return flag==ids.length?true:false;
    }

    @Override
    public AjaxResult checkLogin(SysUser user) {
        SysUserExample ex = new SysUserExample();
        SysUserExample.Criteria criteria = ex.createCriteria();
        criteria.andLoginAccountEqualTo(user.getLoginAccount());
        // 根据用户名查找
        List<SysUser> users = mapper.selectByExample(ex);
        if(users==null || users.size()==0){
            return AjaxResult.error(501,"用于名不存在");
        }

        // 没找见  返回 用户名不存在
        SysUser user1 = users.get(0);
        // 匹配密码
        if(!user.getLoginPwd().equals(user1.getLoginPwd())){
            return AjaxResult.error(502,"密码不正确");
        }
        // 匹配失败 返回密码错误

        // 返回成功
        return AjaxResult.success(user1);

    }
}
